package com.sg.service.biz.framework.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.common.util.RedisUtil;
import com.sg.dto.base.framework.req.BatchQueryBeginMemberListReq;
import com.sg.dto.base.framework.req.BatchQuerySubordinateInductionRecordReq;
import com.sg.dto.base.framework.req.QuerySubordinateInductionRecordListReq;
import com.sg.dto.biz.framework.req.*;
import com.sg.dto.biz.framework.res.*;
import com.sg.entity.InductionRecord;
import com.sg.entity.OriginalRoleMember;
import com.sg.service.base.framework.MInductionRecordService;
import com.sg.service.base.framework.MOriginalRoleMemberService;
import com.sg.service.biz.framework.InductionRecordService;
import com.sg.service.biz.framework.UserService;
import com.sg.common.exception.Assert;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ：
 * @version 1.0
 * @date ：
 */
@Service
public class DashboardOrgServiceImpl
        implements com.wicket.okrframework.biz.service.DashboardOrgService {

    @Resource
    private RedisUtil redisUtil;
    @Resource
    private MInductionRecordService mInductionRecordService;
    @Resource
    private MOriginalRoleMemberService mOriginalRoleMemberService;
    @Resource
    private UserService userService;
    //@Resource
    //private MCustomFields2Service mCustomFields2Service;
    @Resource
    private NbDashboardOrg nbDashboardOrg;
    @Resource
    private InductionRecordService inductionRecordService;

    /**
     * D1查询我下属就职记录列表(公共)[9031]
     * gen by moon at 4/1/2024, 1:38:45 AM
     */
    @Trace(operationName = "D1查询我下属就职记录列表(公共)")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Transactional(rollbackFor = Exception.class)
    @Override
    public QuerySubordinateInductionRecordListComRespDto querySubordinateInductionRecordListCom(QuerySubordinateInductionRecordListComReqDto reqDto) {


        List<InductionRecord> listInductionRecord_1 = new ArrayList<>();
//步骤0: 1-3-13查询我下属就职记录列表 - querySubordinateInductionRecordList
        List<InductionRecord> listInductionRecord = new ArrayList<>();
        QuerySubordinateInductionRecordListReq querySubordinateInductionRecordListReq = new QuerySubordinateInductionRecordListReq();
        querySubordinateInductionRecordListReq.setSubjectLifeCycle("CURRENTLY_VALID");//sourceId:1682608_1
        if (reqDto != null) {
            querySubordinateInductionRecordListReq.setDirectorInductionRecordId(reqDto.getDirectorInductionRecordId());//SimpleFieldAssign//sourceId:1682599_1
            querySubordinateInductionRecordListReq.setSpaceId(reqDto.getSpaceId());//SimpleFieldAssign//sourceId:1682601_1
        }

        /*1-3-13查询我下属就职记录列表[9032]   */
        Assert.isNull(querySubordinateInductionRecordListReq.getSubjectLifeCycle(), "D1查询我下属就职记录列表(公共)-1-3-13查询我下属就职记录列表-主体生命周期不能为空", false);
        Assert.isNull(querySubordinateInductionRecordListReq.getSpaceId(), "D1查询我下属就职记录列表(公共)-1-3-13查询我下属就职记录列表-创建于空间ID不能为空", false);
        listInductionRecord = mInductionRecordService.querySubordinateInductionRecordList(querySubordinateInductionRecordListReq)/*vcase invoke 本地 method 方法调用;*/;


        listInductionRecord_1 = listInductionRecord;

        QuerySubordinateInductionRecordListComRespDto retData = new QuerySubordinateInductionRecordListComRespDto();
        retData.setOrgInductionRecordList(listInductionRecord_1.stream().map(item -> BeanUtil.toBean(item, OrgInductionRecordDto.class)).collect(Collectors.toList()));//objList-to-objLists//sourceId:1682619_1


        return retData;
    }

    /**
     * D1批量查询个人资料By身份人员(公共)[9064]
     * gen by moon at 4/2/2024, 5:40:38 PM
     */
    @Trace(operationName = "D1批量查询个人资料By身份人员(公共)")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Transactional(rollbackFor = Exception.class)
    @Override
    public BatchQueryUserinfoByMemberComRespDto batchQueryUserinfoByMemberCom(BatchQueryUserinfoByMemberComReqDto reqDto) {


        List<OriginalRoleMember> listOriginalRoleMember_1 = new ArrayList<>();
        BatchQueryUserInfoComRespDto batchQueryUserInfoComRespDto_1 = null;
//步骤0: 1-2-09批量查询身份人员列表	 - batchQueryBeginMemberList
        List<OriginalRoleMember> listOriginalRoleMember = new ArrayList<>();
        BatchQueryBeginMemberListReq batchQueryBeginMemberListReq = new BatchQueryBeginMemberListReq();
        batchQueryBeginMemberListReq.setSubjectLifeCycle("CURRENTLY_VALID");//sourceId:1686257_1
        if (reqDto != null) {
            batchQueryBeginMemberListReq.setBeginMemberList(reqDto.getBeginMemberList());//list-field-assign//sourceId:1686253_1
            batchQueryBeginMemberListReq.setSpaceId(reqDto.getSpaceId());//SimpleFieldAssign//sourceId:1686260_1
        }

        /*1-2-09批量查询身份人员列表	[1181]   */
        Assert.isNull(batchQueryBeginMemberListReq.getSubjectLifeCycle(), "D1批量查询个人资料By身份人员(公共)-1-2-09批量查询身份人员列表	-主体生命周期不能为空", false);
        Assert.isNull(batchQueryBeginMemberListReq.getSpaceId(), "D1批量查询个人资料By身份人员(公共)-1-2-09批量查询身份人员列表	-创建于空间ID不能为空", false);
        listOriginalRoleMember = mOriginalRoleMemberService.batchQueryBeginMemberList(batchQueryBeginMemberListReq)/*vcase invoke 本地 method 方法调用;*/;


        listOriginalRoleMember_1 = listOriginalRoleMember;

//步骤1: D1批量查询个人资料(公共) - batchQueryUserInfoCom
        BatchQueryUserInfoComRespDto batchQueryUserInfoComRespDto = null;
        if (listOriginalRoleMember != null && !CollectionUtil.isEmpty(listOriginalRoleMember) && listOriginalRoleMember.size() > 0) {
            BatchQueryUserInfoComReqDto batchQueryUserInfoComReqDto = new BatchQueryUserInfoComReqDto();
            if (listOriginalRoleMember != null && !CollectionUtil.isEmpty(listOriginalRoleMember) && listOriginalRoleMember != null && !CollectionUtil.isEmpty(listOriginalRoleMember)) {
                batchQueryUserInfoComReqDto.setUserInfoList(listOriginalRoleMember.stream().map(item -> item.getUserId())
                        .collect(Collectors.toList()));/*list-to-strings*///sourceId:1686267_1
            }

            /*D1批量查询个人资料(公共)[8002]   */

            batchQueryUserInfoComRespDto = userService.batchQueryUserInfoCom(batchQueryUserInfoComReqDto)/*vcase invoke isSameApp*/;


            batchQueryUserInfoComRespDto_1 = batchQueryUserInfoComRespDto;
        }

        BatchQueryUserinfoByMemberComRespDto retData = new BatchQueryUserinfoByMemberComRespDto();
        //数据集融合  MoreListToOneSource
        if (listOriginalRoleMember_1 != null && !CollectionUtil.isEmpty(listOriginalRoleMember_1) && !CollectionUtil.isEmpty(listOriginalRoleMember_1)) {
            for (OriginalRoleMember originalRoleMember : listOriginalRoleMember_1) {
                BeginMemberDto retElm = new BeginMemberDto();
                retElm.setOriginalRoleMemberId(originalRoleMember.getOriginalRoleMemberId());//SimpleFieldAssign//sourceId:330003_2
                retElm.setUserId(originalRoleMember.getUserId());//SimpleFieldAssign//sourceId:330004_2
                retData.getBeginMemberList().add(retElm);
                if (batchQueryUserInfoComRespDto_1 != null) {
                    for (UserInfoDto userInfoDto : batchQueryUserInfoComRespDto_1.getUserInfoList()) {
                        if (userInfoDto.getUserId().equals(originalRoleMember.getUserId())) {
                            if (batchQueryUserInfoComRespDto_1 != null) {
                                retElm.setIdCardName(userInfoDto.getIdCardName());//SimpleFieldAssign//sourceId:330005_2
                                retElm.setNickName(userInfoDto.getNickName());//SimpleFieldAssign//sourceId:330006_2
                                retElm.setUserFace(userInfoDto.getUserFace());//SimpleFieldAssign//sourceId:330007_2
                            }
                        }
                    }
                }

            }
        }//sourceId:1686269_1


        return retData;
    }

    /**
     * D1批量查询我下属就职记录(公共)[10050]
     * gen by moon at 7/25/2024, 1:11:47 AM
     */
    @Trace(operationName = "D1批量查询我下属就职记录(公共)")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Transactional(rollbackFor = Exception.class)
    @Override
    public BatchQuerySubordinateInductionRecordComRespDto batchQuerySubordinateInductionRecordCom(BatchQuerySubordinateInductionRecordComReqDto reqDto) {


        List<InductionRecord> listInductionRecord_1 = new ArrayList<>();
//步骤0: 1-3-13批量查询我下属就职记录 - batchQuerySubordinateInductionRecord
        List<InductionRecord> listInductionRecord = new ArrayList<>();
        BatchQuerySubordinateInductionRecordReq batchQuerySubordinateInductionRecordReq = new BatchQuerySubordinateInductionRecordReq();
        if (reqDto != null) {
            batchQuerySubordinateInductionRecordReq.setOrgInductionRecordList(reqDto.getOrgInductionRecordList());//list-field-assign//sourceId:1955307_1
            batchQuerySubordinateInductionRecordReq.setSubjectLifeCycle(reqDto.getSubjectLifeCycle());//SimpleFieldAssign//sourceId:1955308_1
            batchQuerySubordinateInductionRecordReq.setSpaceId(reqDto.getSpaceId());//SimpleFieldAssign//sourceId:1955309_1
        }

        /*1-3-13批量查询我下属就职记录[10051]   */

        listInductionRecord = mInductionRecordService.batchQuerySubordinateInductionRecord(batchQuerySubordinateInductionRecordReq)/*vcase invoke 本地 method 方法调用;*/;


        listInductionRecord_1 = listInductionRecord;

        BatchQuerySubordinateInductionRecordComRespDto retData = new BatchQuerySubordinateInductionRecordComRespDto();
        retData.setOrgInductionRecordList(listInductionRecord_1.stream().map(item -> BeanUtil.toBean(item, OrgInductionRecordDto.class)).collect(Collectors.toList()));//objList-to-objLists//sourceId:1955314_1


        return retData;
    }

    /**
     * D1查询主管下属就职记录列表(公共)[10052]
     * gen by moon at 7/25/2024, 1:12:02 AM
     */
    @Trace(operationName = "D1查询主管下属就职记录列表(公共)")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Transactional(rollbackFor = Exception.class)
    @Override
    public QueryDirectorSubInateInductionRecordListComRespDto queryDirectorSubInateInductionRecordListCom(QueryDirectorSubInateInductionRecordListComReqDto reqDto) {


        BatchQuerySubordinateInductionRecordComRespDto batchQuerySubordinateInductionRecordComRespDto_1 = null;
//步骤0: M1-获取接收字段（特殊方法） - queryReceiveFieldDetail
        //ModelCode: receptionService
        QueryReceiveFieldDetailRespDto receptionServiceRes = null;
        QueryReceiveFieldDetailReqDto receptionServiceReq = new QueryReceiveFieldDetailReqDto();
        if (reqDto != null) {
            receptionServiceReq.setOriginalRoleMemberId(reqDto.getOriginalRoleMemberId());//SimpleFieldAssign//sourceId:1955577_1
        }

        /*M1接收入参字段[3715]  用于特殊方法接收上游入参。 */
        Assert.isNull(receptionServiceReq.getOriginalRoleMemberId(), "D1查询主管下属就职记录列表(公共)-M1接收入参字段-身份人员ID不能为空", false);
        receptionServiceRes = nbDashboardOrg.queryReceiveFieldDetail(receptionServiceReq);


//步骤1: D1查询员工就职记录列表(公共) - queryStaffInductionRecordListCom
        QueryStaffInductionRecordListComRespDto queryStaffInductionRecordListComRespDto = null;
        QueryStaffInductionRecordListComReqDto queryStaffInductionRecordListComReqDto = new QueryStaffInductionRecordListComReqDto();
        if (reqDto != null) {
            queryStaffInductionRecordListComReqDto.setOriginalRoleMemberId(reqDto.getOriginalRoleMemberId());//SimpleFieldAssign//sourceId:1955319_1
        }

        /*D1查询员工就职记录列表(公共)[9978]   */
        Assert.isNull(queryStaffInductionRecordListComReqDto.getOriginalRoleMemberId(), "D1查询主管下属就职记录列表(公共)-D1查询员工就职记录列表(公共)-身份人员ID不能为空", false);
        queryStaffInductionRecordListComRespDto = inductionRecordService.queryStaffInductionRecordListCom(queryStaffInductionRecordListComReqDto)/*vcase invoke isSameApp*/;


//步骤2: D1批量查询我下属就职记录(公共) - batchQuerySubordinateInductionRecordCom
        BatchQuerySubordinateInductionRecordComRespDto batchQuerySubordinateInductionRecordComRespDto = null;
        if (queryStaffInductionRecordListComRespDto != null) {
            BatchQuerySubordinateInductionRecordComReqDto batchQuerySubordinateInductionRecordComReqDto = new BatchQuerySubordinateInductionRecordComReqDto();
            batchQuerySubordinateInductionRecordComReqDto.setSubjectLifeCycle("CURRENTLY_VALID");//sourceId:1955316_1
            batchQuerySubordinateInductionRecordComReqDto.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID//sourceId:1955317_1
            if (queryStaffInductionRecordListComRespDto != null) {
                batchQuerySubordinateInductionRecordComReqDto.setOrgInductionRecordList(queryStaffInductionRecordListComRespDto.getOrgInductionRecordList());//list-field-assign//sourceId:1955315_1
            }

            /*D1批量查询我下属就职记录(公共)[10050]   */
            Assert.isNull(batchQuerySubordinateInductionRecordComReqDto.getSubjectLifeCycle(), "D1查询主管下属就职记录列表(公共)-D1批量查询我下属就职记录(公共)-主体生命周期不能为空", false);
            Assert.isNull(batchQuerySubordinateInductionRecordComReqDto.getSpaceId(), "D1查询主管下属就职记录列表(公共)-D1批量查询我下属就职记录(公共)-创建于空间ID不能为空", false);
            batchQuerySubordinateInductionRecordComRespDto = batchQuerySubordinateInductionRecordCom(batchQuerySubordinateInductionRecordComReqDto)/*vcase invoke 同服务,同domain*/;


            batchQuerySubordinateInductionRecordComRespDto_1 = batchQuerySubordinateInductionRecordComRespDto;
        }

        QueryDirectorSubInateInductionRecordListComRespDto retData = new QueryDirectorSubInateInductionRecordListComRespDto();
        if (batchQuerySubordinateInductionRecordComRespDto_1 != null) {
            retData.setOrgInductionRecordList(batchQuerySubordinateInductionRecordComRespDto_1.getOrgInductionRecordList().stream().map(item -> BeanUtil.toBean(item, OrgInductionRecordDto.class)).collect(Collectors.toList()));//objList-to-objLists//sourceId:1955579_1
        }


        return retData;
    }
    //
}
